<?php

class User
{
	private $info;
	
	private $link;
	
	public function __construct($link)
	{
		$this->link = &$link;
	}
	
	public function login($username, $password, $encrypt = true)
	{
		if($encrypt)
		{
			$password = Password::encrypt($username, $password);
		}
		
		$q = $this->link->query("SELECT * FROM `users` WHERE `username` = '{$this->link->escape($username)}' AND `password` = '{$this->link->escape($password)}' LIMIT 1");
		
		if(!$q)
		{
			return UserLookupCode::SQL_ERROR;
		}
		
		if($this->link->numRows($q) == 0)
		{
			return UserLookupCode::NOT_FOUND;
		}
		
		$this->info = $this->link->fetchArray($q);
		$this->link->freeResult($q);
		
		if($this->get('state') == UserState::BANNED)
		{
			return UserLookupCode::BANNED;
		}
		
		if($this->get('state') == UserState::PENDING)
		{
			return UserLookupCode::PENDING;
		}
		
		if($this->get('state') == UserState::INACTIVE)
		{
			$this->setState(UserState::ACTIVE);
			$this->updateLastLogin();
			$this->update();
		}
		
		return UserLookupCode::SUCCESS;
	}
	
	public function update()
	{
		$q = $this->link->query("SELECT * FROM `users` WHERE `username` = '{$this->get('username')}' AND `password` = '{$this->get('password')}' LIMIT 1");
		
		if(!$q)
		{
			return UserLookupCode::SQL_ERROR;
		}
		
		$this->info = $this->link->fetchArray($q);
		$this->link->freeResult($q);
	}
	
	public function updateLastLogin()
	{
		$q = $this->link->query("UPDATE `users` SET `last_login` = NOW() WHERE `username` = '{$this->get('username')}' AND `password` = '{$this->get('password')}' LIMIT 1");
		
		$this->link->freeResult($q);
	}
	
	public function create($username, $password)
	{
		$q = $this->link->query(
			sprintf(
				"INSERT INTO 
					`users`
					(`username`, `password`, `level`, `state`)
				VALUES
					(%s, %s, %d, %d)",
				$this->link->escape($username), $this->link->escape($password), UserDefaults::LEVEL, UserDefaults::STATE
			)
		);
		
		$good = false;
		
		if($q)
		{
			$good = true;
		}
		
		$this->link->freeResult($q);
		
		return $good;
	}
	
	public function delete()
	{
		$q = $this->link->query("DELETE FROM `users` WHERE `username` = '{$this->get('username')}' AND `password` = '{$this->get('password')}' LIMIT 1");
		
		$this->link->freeResult($q);
	}
	
	public function resetPassword()
	{
		
	}
	
	public function setPassword($password)
	{
		$q = $this->link->query("UPDATE `users` SET `state` = '{$this->link->escape($state)}' WHERE `username` = '{$this->get('username')}' AND `password` = '{$this->get('password')}' LIMIT 1");
		
		$this->link->freeResult($q);
	}
	
	public function setAccessLevel($level)
	{
		$q = $this->link->query("UPDATE `users` SET `level` = '{$this->link->escape($level)}' WHERE `username` = '{$this->get('username')}' AND `password` = '{$this->get('password')}' LIMIT 1");
		
		$this->link->freeResult($q);
	}
	
	public function setEmail($email)
	{
		$q = $this->link->query("UPDATE `users` SET `email` = '{$this->link->escape($email)}' WHERE `username` = '{$this->get('username')}' AND `password` = '{$this->get('password')}' LIMIT 1");
		
		$this->link->freeResult($q);
	}
	
	public function setState($state)
	{
		$q = $this->link->query("UPDATE `users` SET `state` = '{$this->link->escape($state)}' WHERE `username` = '{$this->get('username')}' AND `password` = '{$this->get('password')}' LIMIT 1");
		
		$this->link->freeResult($q);
	}
	
	public function get($key)
	{
		return $this->info[$key];
	}
}

class UserState
{
	const
		ACTIVE		= 1,
		INACTIVE	= 2,
		PENDING		= 3,
		BANNED		= 4;
	
	private function __construct(){}
}

class UserLookupCode
{
	const
		SUCCESS		= 0,
		SQL_ERROR	= 1,
		NOT_FOUND	= 2,
		BANNED		= 3,
		PENDING		= 4;
	
	private function __construct(){}
}

class UserAccessLevel
{
	const
		GUEST		= 0,
		REGISTERED	= 1,
		ADMIN		= 2;
	
	private function __construct(){}
}

class UserDefaults
{
	const
		LEVEL		= 1,
		STATE		= 1;
	
	private function __construct(){}
}

?>